package com.yaiys.jvm;

import java.util.concurrent.Semaphore;

public class SemaphoreTest {




    public static void main(String[] args) {
        Semaphore semaphore=new Semaphore(1);
        for (int i=0;i<10000;i++){
            new Thread(new Task(semaphore,"线程"+i)).start();

        }

    }
    static class Task extends Thread{
         Semaphore semaphore;

        public Task (Semaphore semaphore,String name) {
            this.semaphore=semaphore;
            this.setName(name);

        }

        @Override
        public void run() {
            try {
                semaphore.acquire();//获取公共资源
                System.out.println(this.getName()+"-------获取公共资源--------"+semaphore.getQueueLength());
                Thread.sleep(1000);
                //释放公共咨询
                semaphore.release();

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public void procssThread(){
        new Thread();
    }
}
